﻿using System;
using System.Collections.Generic;

namespace Gmantis.Controls.Chart
{
    internal class StackedSum
    {
        // Fields
        private Dictionary<double, double>[] _dicts;
        private int _nsg;

        // Methods
        public StackedSum(int nsg)
        {
            this._nsg = nsg;
            this._dicts = new Dictionary<double, double>[nsg];
            for (int i = 0; i < nsg; i++)
            {
                this._dicts[i] = new Dictionary<double, double>();
            }
        }

        public void Add(int sg, double x, double y)
        {
            this.Check(sg, false);
            Dictionary<double, double> dictionary = this._dicts[sg];
            if (dictionary.ContainsKey(x))
            {
                Dictionary<double, double> dictionary2;
                double num;
                (dictionary2 = dictionary)[num = x] = dictionary2[num] + y;
            }
            else
            {
                dictionary[x] = y;
            }
        }

        private bool Check(int sg, bool throwException = false)
        {
            if ((sg >= 0) && (sg < this._nsg))
            {
                return true;
            }
            if (throwException)
            {
                throw new ArgumentOutOfRangeException();
            }
            return false;
        }

        public double[] GetYs(int sg)
        {
            this.Check(sg, false);
            Dictionary<double, double> dictionary = this._dicts[sg];
            double[] array = new double[dictionary.Values.Count];
            dictionary.Values.CopyTo(array, 0);
            return array;
        }

        // Properties
        public double this[int sg, double x]
        {
            get
            {
                this.Check(sg, false);
                Dictionary<double, double> dictionary = this._dicts[sg];
                if (dictionary.ContainsKey(x))
                {
                    return dictionary[x];
                }
                return 0.0;
            }
            set
            {
                this.Check(sg, false);
                Dictionary<double, double> dictionary = this._dicts[sg];
                dictionary[x] = value;
            }
        }

    }
}
